package game.controller;

import java.io.FileNotFoundException;

import javax.management.modelmbean.XMLParseException;
import javax.xml.bind.JAXBException;

import xmlHandler.XmlHandler;
import xmlHandler.jaxbGenerated.Clue;
import game.view.console.UserDataRetriever;

public class ClueGame {
	
	private static final int LOAD_PROPERTIES_FROM_USER = 1;
	private static final int LOAD_PROPERTIES_FROM_XML = 2;
	private static final int QUIT = 3;
	
	private static void initGame()
	{
		getGameProperties();		
	}
	
	private static void getGameProperties()
	{
		String propertiesInputType = UserDataRetriever.showStartMenu();
		
		while (!propertiesInputType.trim().equals(Integer.toString(LOAD_PROPERTIES_FROM_USER))
				&& !propertiesInputType.trim().equals(Integer.toString(LOAD_PROPERTIES_FROM_XML))
				&& !propertiesInputType.trim().equals(Integer.toString(QUIT)))
		{
			propertiesInputType = UserDataRetriever.getCorrctedUserInput ();
		}
		
		Integer propertiesInputTypeNum = new Integer (propertiesInputType);
		
		switch (propertiesInputTypeNum) {
		case LOAD_PROPERTIES_FROM_USER:
			loadPropsFromUser();
			break;
		case LOAD_PROPERTIES_FROM_XML:
			loadPropsFromXml("");
			break;
		case QUIT:
			UserDataRetriever.showQuitGameScreen();
			System.exit(0);
			break;
		}
	}
	
	private static void loadPropsFromUser ()
	{
		
	}
	
	private static void loadPropsFromXml (String errorMessage)
	{
		
		String xmlFilePath = UserDataRetriever.getXmlFilePathFromUser(errorMessage);
		Clue clue = null;
		
		try
		{
			clue = XmlHandler.getInstance().readFromXml(xmlFilePath);
		}
		catch (FileNotFoundException fileNotFoundException)
		{
			loadPropsFromXml (fileNotFoundException.getMessage());
		}
		catch (IllegalArgumentException illegalArgumentException)
		{
			loadPropsFromXml (illegalArgumentException.getMessage());
		}
		catch (XMLParseException xmlParseException)
		{
			loadPropsFromXml (xmlParseException.getMessage());
		}
		catch (JAXBException jaxbException)
		{
			loadPropsFromXml (jaxbException.getMessage());
		}
				
		Board.getInstance().initBoardfromXml(clue);
			
		Board.getInstance().print();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		initGame();	
	}

}
